<?php

  class YuangongAction extends CommonAction{
  
    public function index(){
	
	  $where['uid']=array('eq',session('did'));
	
	  $this->re=$this->page('yuangong','10','id desc',$where);
	
	  $this->display();
	
	}
  
  
    public function del(){
	
	  $this->ajaxReturn((M('yuangong')->where('id="%s"',$_POST['id'])->delete())?1:2);
	
	}
  
  
    public function add(){
	
	  if(!empty($_POST)){
	  
	    $num=M('yuangong')->where('uid="%s"',session('did'))->count();
		
		if($num>=5){
		
		  $this->error('每名代理最多添加5名员工');
		
		}
	  
	  
	    $result=M('yuangong')->where('username="%s" and uid="%s"',$_POST['username'],session('did'))->find();
		
		if($result!=null){
		
		  $this->error('员工名称重复');
		
		}
	  
	    $_POST['money']=0;
	    $_POST['uid']=session('did');
	  
	    $re=M('yuangong')->add($_POST);
		
		if($re){
		
		  $this->success('添加成功',U('index'));exit();
		
		}else{
		
		  $this->error('添加失败');
		
		}
		
	  
	  }
	
	
	  $this->display();
	
	}
  
  
  
    public function xiangxi(){
	  
	  $where['status']=array('eq','1');
	  $where['yid']=array('eq',$_GET['id']);
	
	  $re=$this->page('buyer','10','id desc',$where);
	  
	    $i=-1;
	    foreach($re as $v){
		 $i++;
		 
		 $pianku=M('pianku')->where('id="%s"',$v['vid'])->find();
		 $re[$i]['title']=$pianku['title'];
		
		}
	  
	  
	  $this->re=$re;
	  
	  
	  
	
	  $this->display();
	
	}
  
  
  }